package com.zzj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.annotation.CommentCountAnnotation;
import com.zzj.annotation.LogAnnotation;
import com.zzj.base.Result;
import com.zzj.entity.Comment;
import com.zzj.model.dto.CommentCreateDto;
import com.zzj.model.dto.comment.TargetIdCommentDto;
import com.zzj.model.vo.CommentVo;
import com.zzj.service.ICommentService;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-07-17
 */
@RestController
@RequestMapping("/comment")
public class CommentController {


    @Resource
    private ICommentService commentService;

    @LogAnnotation(name = "获取文章的评论", description = "分页获取文章评论 targetID 即为文章ID")
    @Transactional
    @PostMapping("getPostComment")
    public Result<?> getPostComment(@RequestBody @Validated TargetIdCommentDto in) {
        IPage<CommentVo> commentVoIPage = commentService.findByTargetId(new Page<>(in.getPageNo(), in.getPageSize()),
                in.getTargetId(),
                1);
        return Result.success(commentVoIPage);
    }

    @LogAnnotation(name = "添加评论", description = "新增评论")
    @Transactional
    @PostMapping("createPostComment")
    @CommentCountAnnotation
    public Result<?> createPostComment(@RequestBody @Validated CommentCreateDto in) {
        Comment Comment = JSONObject.parseObject(JSON.toJSONString(in), Comment.class);
        Comment.setTargetType(1);
        commentService.save(Comment);
        return Result.success("评论成功");
    }



}
